package com.ls.blog.service;

import com.ls.blog.api.CategoryService;
import com.ls.blog.entity.Category;
import com.ls.blog.mapper.CategoryMapper;
import com.ls.blog.vo.CategoryRecord;
import com.ls.blog.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: myblog
 * @author: lishun
 * @create: 2021-07-11 09:49
 **/
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public CategoryVo findCategoryList() {
        List<CategoryRecord> categories = categoryMapper.findCategoryList();
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setCategoryRecords(categories);
        categoryVo.setCount(categories.size());
        return categoryVo;
    }

    private List<CategoryVo> copyList(List<Category> categories) {
        List<CategoryVo> categoryVos = new ArrayList<>();
        for (Category category:categories){
            categoryVos.add(copy(category));
        }
        return categoryVos;
    }

    private CategoryVo copy(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category,categoryVo);
        return categoryVo;
    }

    @Override
    public List<CategoryVo> categoryDetail() {
        List<Category> categories = categoryMapper.selectList(null);
        List<CategoryVo> categoryVos = copyList(categories);
        return categoryVos;
    }

    @Override
    public CategoryVo categoryDetailById(Long id) {
        Category category = categoryMapper.selectById(id);
        CategoryVo vo = copy(category);
        return vo;
    }
}
